﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
using A2DFramework.InstanceService;
using A2DFramework.LogService.LogProviders;

namespace A2DFramework.LogService.Configuration
{
    public static class LogConfiguration
    {
        public static bool LogEnabled{get;set;}
        public static int LogFileSize{get;set;}
        public static string LogFileName { get; set; }

        public static void Load()
        {
            XElement root = XElement.Load(System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "A2D.config"));

            root = root.Element("LogConfig");
            if (root == null)
                throw new Exception("LogConfig node not exists");

            if (root.Attribute("LogType")==null)
                throw new Exception("LogConfig's LogType attribute not exists");
            if (root.Attribute("LogType").Value != "TextFile")
                throw new Exception("LogConfig's LogType must be TextFile");

            LogEnabled = bool.Parse(root.Element("TextFileLogProvider").Element("LogEnabled").Value);
            LogFileSize = int.Parse(root.Element("TextFileLogProvider").Element("LogFileSize").Value);
            if (root.Element("TextFileLogProvider").Element("LogFileName")!=null)
                LogFileName = root.Element("TextFileLogProvider").Element("LogFileName").Value;

            ObjectFactory.Register<ILog, TextFileLogger>();  
        }
    }
}
